package edu.pku.sei.act.sampletask;

import edu.pku.sei.act.actools.ITask;
import edu.pku.sei.act.sampletask.car._Car;
import edu.pku.sei.act.sampletask.parkinglot._Parkinglot;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class SampleTask extends Activity {
	
	private static ITask sampleTask;
	
	private ServiceConnection TaskConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			sampleTask = ITask.Stub.asInterface(arg1);
		}

		public void onServiceDisconnected(ComponentName name) {
			sampleTask = null;
		}
	};

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_task);
        
        Intent taskIntent = new Intent();
        taskIntent.setAction("edu.pku.sei.act.sampletask.AutonomicParking");
        bindService(taskIntent, TaskConnection, Context.BIND_AUTO_CREATE);
        
        Button act_car = (Button)findViewById(R.id.button1);
        
        act_car.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				try {
					sampleTask.playRole(_Car.ROLE_NAME);
				} catch (RemoteException e) {
					
					e.printStackTrace();
				}
			}
        	
        });
        
        Button act_pl = (Button)findViewById(R.id.button2);
        act_pl.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				try {
					sampleTask.playRole(_Parkinglot.ROLE_NAME);
				} catch (RemoteException e) {
					
					e.printStackTrace();
				}
			}
		});
    }

    @Override
	protected void onDestroy() {
		unbindService(TaskConnection);
		super.onDestroy();
	}

    @Override
    protected void onPause() {
    	//unbindService(TaskConnection);
    	super.onPause();
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_sample_task, menu);
        return true;
    }
}
